package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	import mx.controls.Alert;


	public class Generator extends Sprite
	{
		public var mp3ID:Number;
		public var host:String;
		public var XML_URL:String;
		public var myLoader:URLLoader;
		public var myXMLURL:URLRequest;
		public var parmsXML:XML;
		public var nc:NetConnection;
		public var ns:NetStream;
		public var lowqualitydir:String;
		public var trackPath:String;
		public var qdir:String;
		public var client:Object;

		public function Generator(mp3id:String, client:Object)
		{
			this.client=client;
			host="195.91.237.51";
			parmsXML=new XML();
			XML_URL="http://" + host + "/getmp3parms.php?mp3id=" + mp3id;

			myXMLURL=new URLRequest(XML_URL);
			myLoader=new URLLoader(myXMLURL);
			myLoader.addEventListener("complete", xmlLoaded)
		}

		public function onBWDone(... args):void
		{
			//ignore onBWDone
		}

		public function xmlLoaded(event:Event):void
		{
			var _loc_2:String=null;
			var _loc_3:String=null;
			parmsXML=XML(myLoader.data);
			_loc_2=parmsXML.fname;
			_loc_2=_loc_2.substring(0, _loc_2.length - 4);

			trackPath=_loc_2;
			lowqualitydir=parmsXML.lowqualitydir;
			qdir=lowqualitydir;

			_loc_3=parmsXML.dir;
			if (_loc_3 != "")
			{
				trackPath=_loc_3 + "/" + trackPath;
			}
			nc=new NetConnection();
			nc.client=this;
			nc.objectEncoding=ObjectEncoding.AMF3;
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			nc.connect("rtmp://" + host + "/vod");

			return;
		} // end function

		public function Play()
		{
			ns=new NetStream(nc);
			ns.addEventListener(NetStatusEvent.NET_STATUS, netStreamStatus);

			var trans:SoundTransform;
			trans=new SoundTransform(0.75, 0);
			ns.soundTransform=trans;

			var streamName:String="mp3:" + qdir + trackPath;
			var o_res:Responder=new Responder(onres, respStatus);
			ns.seek(0);
			var vid:Video=new Video();
			vid.attachNetStream(ns);
			ns.play("mp3:" + qdir + trackPath);

//			o_res.onResult=function(n_vLen)
//			{
//				Alert.show(n_vLen + " seconds");
//			}
			nc.call("getLength", o_res, streamName);






			return;
		} // end function

		function onres(n_vLen)
		{
			Alert.show(n_vLen + " seconds");
		}

		function setDuration(nLength:Number):void
		{
			Alert.show(nLength + " seconds");
		}

		public function respStatus(event:Object):void
		{
			Alert.show("> resps status > " + event.code);
		}

		public function netStreamStatus(event:NetStatusEvent):void
		{
			Alert.show("> ns status > " + event.info.code);
		}

		public function netStatus(event:NetStatusEvent):void
		{

			if (event.info.code == "NetConnection.Connect.Success")
			{
				if (ns != null)
				{
					ns.close();
				}
				Play();
			}
			if (event.info.code == "NetConnection.Connect.Rejected" || event.info.code == "NetConnection.Connect.Failed")
			{

			}
			return;
		} // end function

	}
}